<?php

require_once dirname(__FILE__) . '/../lib/friend_referralGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/friend_referralGeneratorHelper.class.php';

/**
 * friend_referral actions.
 *
 * @package    docplus.co.za
 * @subpackage friend_referral
 * @author     Fox Matuku
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class friend_referralActions extends autoFriend_referralActions
{

    /**
     * Executes refer action
     *
     * @param sfRequest $request A request object
     */
    public function executeRefer(sfWebRequest $request)
    {
        if (!$this->getUser()->isAuthenticated())
            die("signin");
        
        $this->friends = 2;
        
        // the customizable default message
        $referral = new Referral();
        $referral->setMessage("Hi, I'm using DocPlus, and think you should too. It's a revolutionary free service where I get personalized health information and answers to my health questions from thousands of leading RSA physicians.You should check it out!");
        
        $this->form = new ReferralForm($referral, array('friends' => $this->friends));
        if ($request->isMethod('post'))
        {
            //
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                try
                {
                    // send the friends emails
                    $referral = $this->form->save();
                    $this->getUser()->setFlash('notice', 'Thank you for inviting your friends to DocPlus');
                    
                    $this->redirect("@refer_friend");
                }
                catch (Exception $e)
                {
                    die($e);
                    // invoke the event here so we get the error via email
                }
            }
            else
            {
//                for ($i = -1; $i < $this->friends; $i++)
//                {
//                    if ($i == -1)
//                    {
//                        foreach ($this->form as $field)
//                        {
//                            echo $field->renderLabel() . " " . $field->renderError() . "<br />";
//                        }
//                    }
//                    else
//                    {
//                        foreach ($this->form[$i] as $field)
//                        {
//                            echo $field->renderLabel() . " " . $field->renderError() . "<br />";
//                            echo $this->form->renderGlobalErrors() . "<br />";
//                        }
//                    }
//                }
//                die($this->form->renderGlobalErrors());
            }
        }
    }

}
